﻿using System;
using System.Windows;
using System.Windows.Input;
using System.Windows.Controls;
using System.Collections.ObjectModel;

using SBPweb.Forge.Controls.Silverlight.Utilities;

using SBPweb.Communication.Shared.Library.Contacts;
using SBPweb.Communication.Shared.Library.Profiles;

using SBPweb.Communication.Client.Library.Xmpp.Profiles;

using SBPweb.Communication.Client.Controls.Xmpp.Themes;

namespace SBPweb.Communication.Client.Controls.Xmpp.Contacts
{

    public partial class ContactsView : UserControlBase
    {

        #region Private fields

        private ObservableCollection<ContactBase> selectedContacts = new ObservableCollection<ContactBase>();
        
        #endregion

        #region Constructor logic

        public ContactsView()
            : base()
        {
            IsContextMenuEnabled = true;
            InitializeComponent();
            Style = Generic.Instance["xmppContactsView"] as Style;
            if (ItemHeight == 0)
            {
                ItemHeight = 32;
            }

            if (Messenger != null)
            {
                Messenger.Initialized += new EventHandler(Messenger_Initialized);
                Messenger.Contacts.ContactListReceived += new EventHandler(Contacts_ContactListReceived);
            }
            if (string.IsNullOrEmpty(ImageSourceBindingPathFormat))
            {
                ImageSourceBindingPathFormat = "/Resources/Contact_{0}_64x64.png";
            }
        }

        #endregion

        #region Property accessors

        public string ImageSourceBindingPathFormat
        {
            get
            {
                if (imageSourceConverter == null)
                {
                    imageSourceConverter = Resources["imageSourceConverter"] as ImageSourceConverter;
                }
                return imageSourceConverter.ImageSourceBindingPathFormat;
            }
            set
            {
                imageSourceConverter.ImageSourceBindingPathFormat = value;
            }
        }

        public double ItemHeight
        {
            get;
            set;
        }

        public SelectionMode ItemSelectMode
        {
            get
            {
                return lbContacts.SelectionMode;
            }
            set
            {
                lbContacts.SelectionMode = value;
            }
        }

        public bool IsContextMenuEnabled
        {
            get;
            set;
        }

        public ObservableCollection<ContactBase> SelectedContacts
        {
            get
            {
                lock (selectedContacts)
                {
                    return selectedContacts;
                }
            }
        }

        #endregion

        #region Events

        public event SelectionChangedEventHandler ItemSelectionChanged
        {
            add
            {
                lbContacts.SelectionChanged += value;
            }
            remove
            {
                lbContacts.SelectionChanged -= value;
            }
        }

        #endregion

        #region Methods

        #region Event handlers

        private void Messenger_Initialized(object sender, EventArgs e)
        {
        }

        private void Contacts_ContactListReceived(object sender, EventArgs e)
        {
            lbContacts.ItemsSource = Messenger.Contacts.ContactList;
        }

        private void ListBox_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (IsContextMenuEnabled)
            {
                Point p = e.GetPosition(lbContacts);
                popupMenu.SetValue(Canvas.LeftProperty, p.X);
                popupMenu.SetValue(Canvas.TopProperty, p.Y);
                popupMenu.Show();
            }
        }

        private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            lock (selectedContacts)
            {
                foreach (ContactBase cb in e.AddedItems)
                {
                    selectedContacts.Add(cb);
                }
                foreach (ContactBase cb in e.RemovedItems)
                {
                    selectedContacts.Remove(cb);
                }
            }
        }

        private void popupMenu_ItemSelected(object sender, Liquid.MenuEventArgs e)
        {
            switch (e.Tag.ToString())
            {
                default:
                    break;
                case "pmiConversation":
                    Messenger.Conversations.OpenConversationWith(SelectedContacts[0]);
                    break;
            }
            popupMenu.Hide();
        }

        //private void cbActions_SelectionChanged(object sender, SelectionChangedEventArgs e)
        //{
        //    ComboBox combo = sender as ComboBox;
        //    ComboBoxItem item = combo.SelectedItem as ComboBoxItem;
        //    ContactBase contact = combo.DataContext as ContactBase;
        //    if (item.Name == "NewConv")
        //    {
        //        Messenger.Conversations.OpenConversationWith(contact);
        //    }
        //    else if (item.Name == "GetProfile")
        //    {
        //        Messenger.Profiles.RequestProfile(contact, new EventHandler<ProfileBaseEventArgs>(Get));
        //    }
        //}

        //private void Get(object sender, ProfileBaseEventArgs e)
        //{
        //    string s = e.Profile.FullName;
        //}

        //internal void OnItemActionsSelectionChanged(object sender, SelectionChangedEventArgs e)
        //{
        //    if (ItemActionsSelectionChanged != null)
        //    {
        //        ItemActionsSelectionChanged(sender, e);
        //    }
        //}

        #endregion

        #endregion

    }

}